package com.etop.weibo.dao;

import com.etop.weibo.beans.Secret;
import com.etop.weibo.beans.User;
import com.etop.weibo.util.WeiBoException;
import com.etop.weibo.util.HibernateUtil;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.Session;

public class SecretDao extends BaseDao<Secret> {
    public final static String ID="id";
    public final static String FROM_ID="fromId";
    public final static String TO_ID="toId";
    public final static String WHAT="what";
    public final static String CREATE_TIME="createTime";
    public final static String IS_NEW="isNew";
    
    
    public void allHasRead(int toId) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.createSQLQuery("update t_secret set isNew=false where to_id=:toId and isNew=true")
                    .setInteger("toId", toId)
                    .executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public int hasNew(int toId, int fromId) {
        Session session = HibernateUtil.openSession();
        try {
            Number count = (Number) session.createSQLQuery("select count(*) from t_secret where to_id=? and from_id=? and isNew=true")
                    .setInteger(0, toId)
                    .setInteger(1, fromId)
                    .uniqueResult();
            return count.intValue();
        } catch (ClassCastException e) {
            throw new WeiBoException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public int hasNew(int toId) {
        Session session = HibernateUtil.openSession();
        try {
            Number count = (Number) session.createSQLQuery("select count(*) from t_secret where to_id=? and isNew=true")
                    .setInteger(0, toId)
                    .uniqueResult();
            return count.intValue();
        } catch (ClassCastException e) {
            throw new WeiBoException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public List<User> userList(int userId) {
        Session session = HibernateUtil.openSession();
        List<User> list = session.createQuery("from User user where "
                + "user.id in (select se.toId from Secret se where se.fromId=:userId1) "
                + "or user.id in (select se.fromId from Secret se where se.toId =:userId2)")
                .setInteger("userId1", userId)
                .setInteger("userId2", userId)
                .list();
        session.close();
        return list;
    }
    
    public List<Secret> secretList(int userId, int withWhoseId) {
        Session session = HibernateUtil.openSession();
        List<Secret> list = session.createQuery("from Secret s where (s.toId=:toId1 and s.fromId=:fromId1)"
                + " or (s.toId=:toId2 and s.fromId=:fromId2)")
                .setInteger("toId1", userId)
                .setInteger("fromId1", withWhoseId)
                .setInteger("toId2", withWhoseId)
                .setInteger("fromId2", userId)
                .list();
        session.close();
        return list;
    }
}
